package com.example.hospitalbakcends.controller;


import com.example.hospitalbakcends.domain.Doctor;
import com.example.hospitalbakcends.domain.request.DoctorBaseRequest;
import com.example.hospitalbakcends.service.DoctorService;
import com.example.hospitalbakcends.utils.RoleCheck;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/doctor")
public class DoctorController {
    @Resource
    private DoctorService doctorService;

    /**
     * 医生登录
     * @param loginRequest
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/login")
    public Doctor doctorLogin(@RequestBody DoctorBaseRequest loginRequest, HttpServletRequest httpServletRequest){
        if(loginRequest==null){
            return null;
        }
        String doctorAccount=loginRequest.getDoctorAccount();
        String doctorPassword=loginRequest.getDoctorPassword();
        if (StringUtils.isAnyBlank(doctorAccount, doctorPassword)) {
            return null;
        }
        return doctorService.doctorLogin(doctorAccount, doctorPassword, httpServletRequest);
    }

    /**
     * 医生注册
     * @param registerRequest
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/register")
    public String doctorRegister(@RequestBody DoctorBaseRequest registerRequest,HttpServletRequest httpServletRequest){
        if(registerRequest==null){
            return null;
        }

        String doctorName=registerRequest.getDoctorName();
        String doctorPassword=registerRequest.getDoctorPassword();
        String doctorCheckPwd=registerRequest.getCheckPassword();
        String department = registerRequest.getDepartment();
        if(!StringUtils.isNoneBlank(doctorName,doctorPassword,doctorCheckPwd)){
            return null;
        }

        String newAccount=doctorService.doctorRegister(doctorName,doctorPassword,doctorCheckPwd,department);
        return newAccount;
    }
    //通过部门查询医生
    @GetMapping("/Bydepartment")
    public List<Doctor> Bydepartment(@RequestParam String department){
        System.out.println("22");
        return doctorService.Bydepartmentlist(department);
    }
    /**
     * 医生更新
     * @param doctor
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/update")
    public int doctorUpdate(@RequestBody Doctor doctor,HttpServletRequest httpServletRequest){
        if(doctor==null){
            return -1;
        }
        doctorService.updateById(doctor);
        return 1;
    }

    /**
     * 医生删除
     * @param id
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/delete")
    public boolean doctorDelete(@RequestBody int id,HttpServletRequest httpServletRequest){
        if (!RoleCheck.isAdmin(httpServletRequest)) {
            return false;
        }
        return doctorService.removeById(id);
    }

    /**
     * 查询所有医生信息
     * @return
     */
    @GetMapping ("/AllDoctorInfo")
    public List<Doctor> getAllDiagnosisInfo(){

        return doctorService.list();
    }

    /**
     * 根据姓名查询医生信息
     * @param doctorName
     * @return
     */
    @PostMapping("/DoctorInfo")
    public Doctor getDiagnosisById(@RequestBody String doctorName){
        if(doctorName==null){
            return null;
        }
        return doctorService.serchDoctorByName(doctorName);
    }


    @GetMapping("/test")
    public int test(){
        System.out.println("coming");
        return 1000;
    }
}
